package com.jsh.erp.controller;

import com.jsh.erp.datasource.entities.Bank;
import com.jsh.erp.service.bank.BankService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author: wy
* @Description:
* @DateTime: 2022-12-08 15:59
* @Params:
* @Return
*/
@RestController
@RequestMapping(value = "/bank")
@Api(tags = {"银行管理"})
public class BankController {
    private Logger logger = LoggerFactory.getLogger(BankController.class);

    @Resource
    private BankService bankService;
    @Resource
    private RedisService redisService;
    /**
     * 全部数据列表
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/getAllList")
    @ApiOperation(value = "全部数据列表")
    public BaseResponseInfo getAllList(HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            List<Bank> bankList = bankService.getBank();
            map.put("bankList", bankList);
            res.code = 200;
            res.data = bankList;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * 根据Id获取地址信息
     * @param bankIds
     * @param request
     * @return
     */
    @GetMapping(value = "/getBankByIds")
    @ApiOperation(value = "根据Id获取地址信息")
    public BaseResponseInfo getBankByIds(@RequestParam("bankIds") String bankIds,
                                           HttpServletRequest request)throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            Map<String,String> bankMap = bankService.getBankMap();
            String names = bankService.getBankByMapAndIds(bankMap, bankIds);
            map.put("names", names);
            res.code = 200;
            res.data = map;
        } catch(Exception e){
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

}
